﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: Propietario			*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.Data;
using ESF.SSO.Core.Entities;
using System.Data;
using ESF.General;
using ESF.General.Entity;
using ESF.SSO.Core.DataAccess.BasicReferences;

#endregion


namespace ESF.SSO.Core.DataAccess
{
	public static class AdoContactUs
	{
		#region Read

		public static BrowseResults<ContactUs> Browse(ContactUs pFilter, PaggingInfo pPagInfo, out int pReadyCount, out int pPendingCount)
		{
			IDbCommand cmd;

			DataRow drResumeCount;

			BrowseResults<ContactUs> result;


			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_BrowseContactUs");

			AdoESFSSO.R.AddParameter (cmd, "pUserAlias", pFilter.filUserAlias, General.Text.ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter(cmd, "pUserFullName", pFilter.filUserFullName, General.Text.ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter(cmd, "pUserIdentificationNumber", pFilter.filUserIdentificationNumber, General.Text.ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddIDParameter(cmd, "pUserIdentificationTypeID", pFilter.filUserIdentificationTypeID);
			AdoESFSSO.R.AddParameter (cmd, "pSenderEmail", pFilter.SenderEmail, General.Text.ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter (cmd, "pMessageStartDate", pFilter.filMessageStartDate);
			AdoESFSSO.R.AddParameter (cmd, "pMessageEndDate", pFilter.filMessageEndDate);
			AdoESFSSO.R.AddIDParameter (cmd, "pContactUsTypeID", pFilter.ContactUsTypeID);
			AdoESFSSO.R.AddParameter (cmd, "pWithResponse", pFilter.filWithResponse);
			AdoESFSSO.R.AddParameter(cmd, "pExtraValue", pFilter.filExtraValue);

			AdoESFSSO.R.AddParametersPagging (cmd, pPagInfo);

			result = AdoESFSSO.R.GetBrowse <ContactUs> (cmd, ConvertTo, pPagInfo);


			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_ResumeProcessedContactUs");

			drResumeCount = AdoESFSSO.R.GetFirstRow (cmd);

			pReadyCount = DataHelper.GetInt (drResumeCount, "ReadyCount", 0);
			pPendingCount = DataHelper.GetInt(drResumeCount, "PendingCount", 0);


			return result;
		}


		public static ContactUs ConvertTo (IDataReader pReader, string pPrefix = "", string pListLoading = "")
		{
			ContactUs result;


			result = new ContactUs ()
			{
				ID = DataHelper.GetString (pReader, pPrefix + "ID"),
				calRowIndex = DataHelper.GetInt (pReader, pPrefix + "Ri"),
				CreationDateTime = DataHelper.GetNullableDateTime (pReader, pPrefix + "CreationDateTime")
				,SenderEmail = DataHelper.GetString (pReader, pPrefix + "SenderEmail")
				,Title = DataHelper.GetString (pReader, pPrefix + "Title")
				,Comments = DataHelper.GetString (pReader, pPrefix + "Comments")
				,Response = DataHelper.GetString (pReader, pPrefix + "Response")
				,ResponseUserAlias = DataHelper.GetString (pReader, pPrefix + "ResponseUserAlias")
				,ResponseDateTime = DataHelper.GetNullableDateTime (pReader, pPrefix + "ResponseDateTime")
				,filUserAlias = DataHelper.GetString (pReader, pPrefix + "calUserAlias")
				,ContactUsTypeID = DataHelper.ConvertToDirectBasicReference (pReader, "Cut_")
			};

			return result;
		}

		#endregion


		#region Write

		public static void InsertResponse (ContactUs pContactUs)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_InsertResponseContactUs");

			AdoESFSSO.W.AddParameter (cmd, "pID", pContactUs.ID);
			AdoESFSSO.W.AddParameter (cmd, "pResponse", pContactUs.Response);
			AdoESFSSO.W.AddParameter (cmd, "pResponseUserAlias", pContactUs.ResponseUserAlias);

			AdoESFSSO.W.Execute (cmd);
		}


		public static void Insert (ContactUs pContactUs)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.W.CreateSPCommand ("[esf_sso].P_InsertContactUs");
			
			AdoESFSSO.W.AddParameter (cmd, "pApplicationCode", pContactUs.Application.Code);
			AdoESFSSO.W.AddParameter (cmd, "pComments", pContactUs.Comments);
			AdoESFSSO.W.AddParameter (cmd, "pTitle", pContactUs.Title);
			AdoESFSSO.W.AddParameter (cmd, "pSenderEmail", pContactUs.SenderEmail);
			AdoESFSSO.W.AddIDParameter (cmd, "pContactUsTypeID", pContactUs.ContactUsTypeID);
			
			AdoESFSSO.W.Execute (cmd);
		}

		#endregion
	}
}
